#!/usr/bin/ruby
require "rubygems"
require "opengl"
require "glut"
require "m3g"
require "pp"

class MyGLUTWidget
  def initialize
    GLUT.Init
    GLUT.InitWindowSize  256, 256
    GLUT.InitDisplayMode GLUT::GLUT_DOUBLE | GLUT::GLUT_RGB | GLUT::GLUT_DEPTH
    GLUT.CreateWindow "Ruby-M3G"

    initialize_scene

    GLUT.ReshapeFunc  method(:reshape).to_proc()
    GLUT.DisplayFunc  method(:display).to_proc()
    GLUT.KeyboardFunc method(:keyboard).to_proc()
    GLUT.MouseFunc    method(:mouse).to_proc()
    GLUT.IdleFunc     method(:idle).to_proc()

  end

  def initialize_scene

    keyframe_sequence = M3G::KeyframeSequence.new 4, 2, M3G::KeyframeSequence::LINEAR
    keyframe_weights = [[0,0], [1,0], [0,1], [0,0]]
    keyframe_sequence.keyframe[0] = 0,   keyframe_weights[0]
    keyframe_sequence.keyframe[1] = 100, keyframe_weights[1]
    keyframe_sequence.keyframe[2] = 200, keyframe_weights[2]
    keyframe_sequence.keyframe[3] = 300, keyframe_weights[3]
    keyframe_sequence.valid_range = 0, 3
    keyframe_sequence.duration    = 400
    keyframe_sequence.repeat_mode = M3G::KeyframeSequence::LOOP

    animation_controller = M3G::AnimationController.new
    animation_track = M3G::AnimationTrack.new keyframe_sequence, M3G::AnimationTrack::MORPH_WEIGHTS
    animation_track.controller = animation_controller

    scale = 1
    bias  = [0,0,0]
    
    base_positions = M3G::VertexArray.new 4, 3, 4
    base_positions.values[0,4] =  [1,-1,0, 1,1,0, -1,-1,0, -1,1,0]

    target1_positions = M3G::VertexArray.new 4, 3, 4
    target1_positions.values[0,4] = [1,-1,0, 0,0,0, -1,-1,0, 0,0,0]
    
    target2_positions = M3G::VertexArray.new 4, 3, 4
    target2_positions.values[0,4] = [2,-1,0, 2,-0.5,0, -2,-1,0, -2,-0.5,0]

    base_vertices = M3G::VertexBuffer.new
    base_vertices.positions = base_positions, scale, bias

    target_vertices = [M3G::VertexBuffer.new, M3G::VertexBuffer.new]
    target_vertices[0].positions = target1_positions, scale, bias
    target_vertices[1].positions = target2_positions, scale, bias

    strips = [42]
    tris = M3G::TriangleStripArray.new 0, strips
    
    app = M3G::Appearance.new

    mesh = M3G::MorphingMesh.new base_vertices, target_vertices, tris, app
    
    weights = [0,0]
    mesh.weights = weights
    mesh.animation_track << animation_track

    cam = M3G::Camera.new
    cam.translate 0,0,10

    @wld = M3G::World.new
    @wld.child << cam
    @wld.child << mesh
    @wld.active_camera = cam
    
    @world_time = 0
  end

  def display
    g3d = M3G::Graphics3D::instance
    g3d.render @wld
    GLUT.SwapBuffers()
  end
  
  def reshape width, height
    g3d = M3G::Graphics3D::instance
    g3d.viewport = 0,0,width,height
    @wld.active_camera.perspective = 45, width/(height*1.0), 0.1, 1000
  end

  def keyboard key, x, y
    if key == 113
      exit
    elsif key == 32
      puts "main: time = #{@world_time}"
      @wld.animate @world_time
      @world_time += 20
    end
    GLUT.PostRedisplay
  end

  def mouse button, state, x, y
  end

  def idle
    GLUT.PostRedisplay
  end

  def start
    GLUT.MainLoop
  end

end


MyGLUTWidget.new.start

